{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1197af31",
   "metadata": {},
   "source": [
    "# Rainbow options with Direct Amplitude Loading\n",
    "In this Notebook we will go through the implementation of the Direct Amplitude Loading Method for the rainbow option presented in [[1]](#QALROP)"
   ]
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### Introduction\n",
    "\n",
    "In finance, a crucial aspect of asset pricing pertains to derivatives.\n",
    "Derivatives are contracts whose value is contingent upon another source, known as the underlying.\n",
    "The pricing of options, a specific derivative instrument, involves determining the fair market value (discounted payoff) of contracts affording their holders the right, though not the obligation, to buy (call) or sell (put) one or more underlying assets at a predefined strike price by a specified future expiration date (maturity date).\n",
    "This process relies on mathematical models, considering variables like current asset prices, time to expiration, volatility, and interest rates."
   ],
   "id": "f63599e2e3afbd49"
  },
  {
   "cell_type": "markdown",
   "id": "95faeb3c",
   "metadata": {},
   "source": [
    "## Data Definitions\n",
    "\n",
    "The problem inputs are:\n",
    "- `NUM_QUBITS`: the number of qubits representing an underlying asset\n",
    "- `NUM_ASSETS`: the number of underlying assets\n",
    "- `K`: the strike price\n",
    "- `S0`: the arrays of underlying assets prices\n",
    "- `dt`: the number of days to the maturity date\n",
    "- `COV`: the covariance matrix that correlate the underlyings\n",
    "- `MU_LOG_RET`: the array containing the mean of the log return of each underlyings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy\n",
    "\n",
    "NUM_QUBITS = 2\n",
    "NUM_ASSETS = 2\n",
    "\n",
    "K = 190\n",
    "S0 = [193.97, 189.12]\n",
    "dt = 250\n",
    "\n",
    "COV = np.array([[0.000335, 0.000257], [0.000257, 0.000418]])\n",
    "MU_LOG_RET = np.array([0.00050963, 0.00062552])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:52.901454Z",
     "start_time": "2024-06-18T13:36:52.729371Z"
    }
   }
  },
  {
   "cell_type": "code",
   "id": "d2d99529",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:54.045061Z",
     "start_time": "2024-06-18T13:36:54.042470Z"
    }
   },
   "source": [
    "MU = MU_LOG_RET * dt\n",
    "CHOLESKY = np.linalg.cholesky(COV) * np.sqrt(dt)\n",
    "SCALING_FACTOR = 1 / CHOLESKY[0, 0]"
   ],
   "outputs": [],
   "execution_count": 13
  },
  {
   "cell_type": "markdown",
   "id": "4be6b880",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-04-01T09:40:28.838996Z",
     "start_time": "2024-04-01T09:40:28.226590Z"
    }
   },
   "source": [
    "## Gaussian State preparation\n",
    "Encode the probability distribution of a discrete multivariate random variable $W$ taking values in $\\{w_0, .., w_{N-1}\\}$ describing the assets' prices at the maturity date. The number of discretized values, denoted as $N$, depends on the precision of the state preparation module and is consequently connected to the number of qubits ($n=$) according to the formula $N=2^n$. \n",
    "\n",
    "$$\n",
    "\\sum_{i=0}^{N-1} \\sqrt{p(w_i)}\\left|w_i\\right\\rangle \n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "id": "172e7a00",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:54.046821Z",
     "start_time": "2024-06-18T13:36:54.042960Z"
    }
   },
   "source": [
    "def gaussian_discretization(num_qubits, mu=0, sigma=1, stds_around_mean_to_include=3):\n",
    "    lower = mu - stds_around_mean_to_include * sigma\n",
    "    upper = mu + stds_around_mean_to_include * sigma\n",
    "    num_of_bins = 2**num_qubits\n",
    "    sample_points = np.linspace(lower, upper, num_of_bins + 1)\n",
    "\n",
    "    def single_gaussian(x: np.ndarray, _mu: float, _sigma: float) -> np.ndarray:\n",
    "        cdf = scipy.stats.norm.cdf(x, loc=_mu, scale=_sigma)\n",
    "        return cdf[1:] - cdf[0:-1]\n",
    "\n",
    "    non_normalized_pmf = (single_gaussian(sample_points, mu, sigma),)\n",
    "    real_probs = non_normalized_pmf / np.sum(non_normalized_pmf)\n",
    "    return sample_points[:-1], real_probs[0].tolist()\n",
    "\n",
    "\n",
    "grid_points, probabilities = gaussian_discretization(NUM_QUBITS)\n",
    "\n",
    "STEP_X = grid_points[1] - grid_points[0]\n",
    "MIN_X = grid_points[0]"
   ],
   "outputs": [],
   "execution_count": 14
  },
  {
   "cell_type": "markdown",
   "id": "151bb5cb",
   "metadata": {},
   "source": [
    "### SANITY CHECK"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbb4f025",
   "metadata": {},
   "source": [
    "The process must be stopped if the strike price $K$ is greater than the maximum value reacheable by the assets during the simulation, to avoid meaningless results. The payoff is $0$ in this case, so there is no need to simulate."
   ]
  },
  {
   "cell_type": "code",
   "id": "5713c295",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:54.051730Z",
     "start_time": "2024-06-18T13:36:54.046283Z"
    }
   },
   "source": [
    "from IPython.display import Markdown\n",
    "\n",
    "if K >= max(S0 * np.exp(np.dot(CHOLESKY, [grid_points[-1]] * 2) + MU)):\n",
    "    display(\n",
    "        Markdown(\n",
    "            \"<font color='red'> K always greater than the maximum asset values. Stop the run, the payoff is 0</font>\"\n",
    "        )\n",
    "    )"
   ],
   "outputs": [],
   "execution_count": 15
  },
  {
   "cell_type": "markdown",
   "id": "ecd5856d",
   "metadata": {},
   "source": [
    "## Maximum Computation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d35363b",
   "metadata": {},
   "source": [
    "### Precision utils"
   ]
  },
  {
   "cell_type": "code",
   "id": "7794ce7c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:54.055944Z",
     "start_time": "2024-06-18T13:36:54.051241Z"
    }
   },
   "source": [
    "FRAC_PLACES = 2\n",
    "\n",
    "\n",
    "def round_factor(a):\n",
    "    precision_factor = 2**FRAC_PLACES\n",
    "    return round(a * precision_factor) / precision_factor\n",
    "\n",
    "\n",
    "def floor_factor(a):\n",
    "    precision_factor = 2**FRAC_PLACES\n",
    "    return np.floor(a * precision_factor) / precision_factor"
   ],
   "outputs": [],
   "execution_count": 16
  },
  {
   "cell_type": "markdown",
   "id": "ed89df19",
   "metadata": {},
   "source": [
    "### Affine and maximum arithmetic definitions\n",
    "\n",
    "Considering the time delta between the starting date ($t_0$) and the maturity date ($t$), we can express the return value $R_i$ for the $i$-th asset as $R_i = \\mu_i + y_i$. Where:"
   ]
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "  $\\mu_i= (t-t_0)\\tilde{\\mu}_i$, being $\\tilde{\\mu}_i$ the expected daily log-return value. It can be estimated by considering the historical time series of log returns for the $i$-th asset."
   ],
   "id": "74a02871489d9ec9"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "$y_i$ is obtained through the dot product between the matrix $\\mathbf{L}$ and the standard multivariate Gaussian sample:\n",
    "\n",
    "$$    y_i = \\Delta x \\cdot \\sum_kl_{ik}d_k + x_{min} \\cdot \\sum_k l_{ik} $$\n",
    "$\\Delta x$ is the Gaussian discretization step, $x_{min}$ is the lower Gaussian truncation value and $d_k \\in [0,2^m-1]$ is the sample taken from the $k$-th standard Gaussian.\n",
    "$l_{ik}$ is the $i,k$ entry of the matrix $\\mathbf{L}$, defined as $\\mathbf{L}=\\mathbf{C}\\sqrt{(t-t_0)}$, where $\\mathbf{C}$ is the lower triangular matrix obtained by applying the Cholesky decomposition to the historical daily log-returns correlation matrix."
   ],
   "id": "6d7e2e6ce33a07ce"
  },
  {
   "cell_type": "code",
   "id": "4f6f55cb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:54.074035Z",
     "start_time": "2024-06-18T13:36:54.056533Z"
    }
   },
   "source": [
    "from functools import reduce\n",
    "\n",
    "from classiq import Output, QNum, get_expression_numeric_attributes, qfunc\n",
    "from classiq.qmod.symbolic import max as qmax\n",
    "\n",
    "a = STEP_X / SCALING_FACTOR\n",
    "b = np.log(S0[0]) + MU[0] + MIN_X * CHOLESKY[0].sum()\n",
    "c = (\n",
    "    SCALING_FACTOR\n",
    "    * (\n",
    "        np.log(S0[1])\n",
    "        + MU[1]\n",
    "        - (np.log(S0[0]) + MU[0])\n",
    "        + MIN_X * sum(CHOLESKY[1] - CHOLESKY[0])\n",
    "    )\n",
    "    / STEP_X\n",
    ")\n",
    "c = round_factor(c)\n",
    "\n",
    "\n",
    "def get_affine_formula(assets, i):\n",
    "    return reduce(\n",
    "        lambda x, y: x + y,\n",
    "        [\n",
    "            assets[j] * round_factor(SCALING_FACTOR * CHOLESKY[i, j])\n",
    "            for j in range(NUM_ASSETS)\n",
    "            if CHOLESKY[i, j]\n",
    "        ],\n",
    "    )\n",
    "\n",
    "\n",
    "def calculate_max_reg_type():\n",
    "    x1 = QNum(\"x1\", NUM_QUBITS, False, 0)\n",
    "    x2 = QNum(\"x2\", NUM_QUBITS, False, 0)\n",
    "    expr = qmax(get_affine_formula([x1, x2], 0), get_affine_formula([x1, x2], 1) + c)\n",
    "    size_in_bits, sign, fraction_digits = get_expression_numeric_attributes(\n",
    "        [x1, x2], expr\n",
    "    )\n",
    "    return size_in_bits, fraction_digits\n",
    "\n",
    "\n",
    "MAX_NUM_QUBITS = calculate_max_reg_type()[0]\n",
    "MAX_FRAC_PLACES = calculate_max_reg_type()[1]"
   ],
   "outputs": [],
   "execution_count": 17
  },
  {
   "cell_type": "code",
   "id": "0074a98c",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:54.074322Z",
     "start_time": "2024-06-18T13:36:54.068844Z"
    }
   },
   "source": [
    "@qfunc\n",
    "def affine_max(x1: QNum, x2: QNum, res: Output[QNum]):\n",
    "    res |= qmax(get_affine_formula([x1, x2], 0), get_affine_formula([x1, x2], 1) + c)"
   ],
   "outputs": [],
   "execution_count": 18
  },
  {
   "cell_type": "markdown",
   "id": "b37716e7",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Direct Method\n",
    "The direct exponential amplitude loading encodes in $\\tilde{f}$ the following function:\n",
    "\\begin{equation}\n",
    "\\tilde{f}(x)=\n",
    "    \\begin{cases}\n",
    "        e^{-a\\hat{x}}, & \\text{if } \\frac{x}{2^P} \\geq \\frac{\\log(K) -b'}{b}\\\\\n",
    "      Ke^{-(b'+ ax_{max})}, & \\text{if } \\frac{x}{2^P} < \\frac{\\log(K) -b'}{b}\n",
    "    \\end{cases}\n",
    "\\end{equation}\n",
    "\n",
    "where $\\hat{x}$ is the binary complement of $x$ ($\\hat{x}=x-x_{max}$) and $x_{max}=2^R-1$, the maximum value that can be stored in $|x\\rangle$ register. For loading $e^{-a\\hat{x}}$ the $|r\\rangle$ is initialized to all zeros and one controlled rotation for each qubit is performed. The rotations angles are: $\\theta_i = 2\\arccos \\left({\\sqrt{e^{-a2^i}}}\\right)$. All the probabilities of getting a $|0\\rangle^{\\otimes{R}}$ in the $|r\\rangle$ are then collected by a multi-controlled X (MCX) gate and stored in the $|1\\rangle$ state of a target qubit."
   ]
  },
  {
   "cell_type": "code",
   "id": "06717f7c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-18T13:36:54.082410Z",
     "start_time": "2024-06-18T13:36:54.075286Z"
    }
   },
   "source": [
    "from classiq import (\n",
    "    RY,\n",
    "    Constraints,\n",
    "    CReal,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    X,\n",
    "    allocate,\n",
    "    apply_to_all,\n",
    "    bind,\n",
    "    control,\n",
    "    create_model,\n",
    "    inplace_prepare_state,\n",
    "    repeat,\n",
    "    show,\n",
    "    synthesize,\n",
    "    within_apply,\n",
    ")\n",
    "from classiq.qmod.symbolic import acos, asin, exp, sqrt\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def exponential_amplitude_loading(\n",
    "    exp_rate: CReal, x: QArray[QBit], aux: QArray[QBit], res: QBit\n",
    ") -> None:\n",
    "    apply_to_all(X, x)\n",
    "    repeat(\n",
    "        x.len,\n",
    "        lambda index: control(\n",
    "            x[index],\n",
    "            lambda: RY(2 * acos(1 / sqrt(exp(exp_rate * (2**index)))), aux[index]),\n",
    "        ),\n",
    "    )\n",
    "    apply_to_all(X, x)\n",
    "\n",
    "    aux_num = QNum(\"aux_num\", aux.len, False, 0)\n",
    "    bind(aux, aux_num)\n",
    "    control(aux_num == 0, lambda: X(res))\n",
    "    bind(aux_num, aux)"
   ],
   "outputs": [],
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting synthesis\n",
      "Opening: http://nightly.platform.classiq.io/circuit/50b6df7b-70a9-4158-841a-89e2e2bd8425?version=0.43.0.dev22\n"
     ]
    }
   ],
   "source": [
    "def get_payoff_expression(x, size, fraction_digits):\n",
    "    payoff = sqrt(\n",
    "        qmax(\n",
    "            S0[0]\n",
    "            * exp(\n",
    "                STEP_X / SCALING_FACTOR * (2 ** (size - fraction_digits)) * x\n",
    "                + (MU[0] + MIN_X * CHOLESKY[0].sum())\n",
    "            ),\n",
    "            K,\n",
    "        )\n",
    "    )\n",
    "    return payoff\n",
    "\n",
    "\n",
    "def get_strike_price_theta_direct(x: QNum):\n",
    "    x_max = 1 - 1 / (2**x.size)\n",
    "    payoff_max = get_payoff_expression(x_max, x.size, x.fraction_digits)\n",
    "\n",
    "    return 2 * asin(np.sqrt(K) / payoff_max)\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def direct_load_amplitudes(geq_reg: QNum, max_reg: QNum, aux_reg: QNum, ind_reg: QBit):\n",
    "    exp_rate = (1 / (2**max_reg.fraction_digits)) * a\n",
    "    control(\n",
    "        geq_reg == 1,\n",
    "        lambda: exponential_amplitude_loading(exp_rate, max_reg, aux_reg, ind_reg),\n",
    "    )\n",
    "    strike_price_theta = get_strike_price_theta_direct(max_reg)\n",
    "    control(geq_reg == 0, lambda: RY(strike_price_theta, ind_reg))\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def direct_payoff(max_reg: QNum, aux_reg: QNum, ind_reg: QBit):\n",
    "    geq_reg = QBit(\"geq_reg\")\n",
    "    within_apply(\n",
    "        lambda: asset_geq_strike_price(max_reg, geq_reg),\n",
    "        lambda: direct_load_amplitudes(geq_reg, max_reg, aux_reg, ind_reg),\n",
    "    )\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def asset_geq_strike_price(\n",
    "    x: QNum,\n",
    "    res: Output[QBit],\n",
    ") -> None:\n",
    "    a = STEP_X / SCALING_FACTOR\n",
    "    b = np.log(S0[0]) + MU[0] + MIN_X * CHOLESKY[0].sum()\n",
    "    COMP_VALUE = (np.log(K) - b) / a\n",
    "    res |= x > floor_factor(COMP_VALUE)\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def rainbow_direct(\n",
    "    x1: QNum,\n",
    "    x2: QNum,\n",
    "    aux_reg: QNum,\n",
    "    ind_reg: QBit,\n",
    ") -> None:\n",
    "    inplace_prepare_state(probabilities, 0, x1)\n",
    "    inplace_prepare_state(probabilities, 0, x2)\n",
    "    max_out = QNum(\"max_out\")\n",
    "    within_apply(\n",
    "        lambda: affine_max(x1, x2, max_out),\n",
    "        lambda: direct_payoff(max_out, aux_reg, ind_reg),\n",
    "    )\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(\n",
    "    x1: Output[QNum],\n",
    "    x2: Output[QNum],\n",
    "    aux_reg: Output[QNum],\n",
    "    ind_reg: Output[QBit],\n",
    ") -> None:\n",
    "    allocate(MAX_NUM_QUBITS, aux_reg)\n",
    "    allocate(NUM_QUBITS, x1)\n",
    "    allocate(NUM_QUBITS, x2)\n",
    "    allocate(1, ind_reg)\n",
    "    rainbow_direct(x1, x2, aux_reg, ind_reg)\n",
    "\n",
    "\n",
    "constraints = Constraints(max_width=23)\n",
    "qmod = create_model(main, constraints=constraints)\n",
    "print(\"Starting synthesis\")\n",
    "qprog = synthesize(qmod)\n",
    "show(qprog)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-18T13:37:06.181557Z",
     "start_time": "2024-06-18T13:36:54.096250Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "id": "661a1859",
   "metadata": {},
   "source": [
    "## IQAE algorithm"
   ]
  },
  {
   "cell_type": "code",
   "id": "ecb49c61",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-18T13:37:06.182864Z",
     "start_time": "2024-06-18T13:37:06.177291Z"
    }
   },
   "source": [
    "from classiq import Z, cfunc\n",
    "from classiq.qmod.builtins.classical_execution_primitives import iqae, save\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def qmci_oracle(ind: QBit):\n",
    "    Z(ind)\n",
    "\n",
    "\n",
    "@cfunc\n",
    "def cmain():\n",
    "    iqae_res = iqae(epsilon=0.05, alpha=0.1)\n",
    "    save({\"iqae_res\": iqae_res})"
   ],
   "outputs": [],
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "id": "3dddfbea",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-18T13:37:06.191736Z",
     "start_time": "2024-06-18T13:37:06.183610Z"
    }
   },
   "source": [
    "from classiq import CInt, QCallable, grover_operator, power\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def grover_algorithm(\n",
    "    k: CInt,\n",
    "    oracle_operand: QCallable[QArray[QBit]],\n",
    "    sp_operand: QCallable[QArray[QBit]],\n",
    "    x: QArray[QBit],\n",
    "):\n",
    "    sp_operand(x)\n",
    "    power(k, lambda: grover_operator(oracle_operand, sp_operand, x))"
   ],
   "outputs": [],
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "id": "19a8b150",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-06-18T13:37:06.199316Z",
     "start_time": "2024-06-18T13:37:06.188686Z"
    }
   },
   "source": [
    "def get_main():\n",
    "    @qfunc\n",
    "    def main(\n",
    "        k: CInt,\n",
    "        ind_reg: Output[QBit],\n",
    "    ) -> None:\n",
    "        full_reg = QArray(\"full_reg\")\n",
    "        allocate(2 * NUM_QUBITS + MAX_NUM_QUBITS + 1, full_reg)\n",
    "        grover_algorithm(\n",
    "            k,\n",
    "            lambda x: qmci_oracle(x[x.len - 1]),\n",
    "            lambda x: rainbow_direct(\n",
    "                x[0:NUM_QUBITS],\n",
    "                x[NUM_QUBITS : 2 * NUM_QUBITS],\n",
    "                x[2 * NUM_QUBITS : 2 * NUM_QUBITS + MAX_NUM_QUBITS],\n",
    "                x[x.len - 1],\n",
    "            ),\n",
    "            full_reg,\n",
    "        )\n",
    "        state_reg = QArray(\"state_reg\")\n",
    "        bind(full_reg, [state_reg, ind_reg])\n",
    "\n",
    "    return main"
   ],
   "outputs": [],
   "execution_count": 23
  },
  {
   "cell_type": "code",
   "id": "355f9bd4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-18T13:37:06.202755Z",
     "start_time": "2024-06-18T13:37:06.194549Z"
    }
   },
   "source": [
    "from classiq import execute, write_qmod\n",
    "\n",
    "\n",
    "def synthesize_and_execute(post_process):\n",
    "    constraints = Constraints(max_width=25)\n",
    "    qmod = create_model(\n",
    "        get_main(),\n",
    "        constraints=constraints,\n",
    "        classical_execution_function=cmain,\n",
    "    )\n",
    "    write_qmod(qmod, \"rainbow_options_direct_method\")\n",
    "    print(\"Starting synthesis\")\n",
    "    qprog = synthesize(qmod)\n",
    "    show(qprog)\n",
    "    print(\"Starting execution\")\n",
    "    res = execute(qprog).result()\n",
    "    iqae_res = res[0].value\n",
    "    parsed_res = post_process(res[0].value)\n",
    "\n",
    "    return (qmod, qprog, iqae_res, parsed_res)"
   ],
   "outputs": [],
   "execution_count": 24
  },
  {
   "cell_type": "markdown",
   "id": "16899f05",
   "metadata": {},
   "source": [
    "## Post Process\n",
    "We need to add to the post-processing function a term:\n",
    "\\begin{equation}\n",
    "\\begin{split}\n",
    "&\\mathbb{E} \\left[\\max\\left(e^{b \\cdot z}, Ke^{-b'}\\right) \\right] e^{b'} - K \\\\\n",
    "= &\\mathbb{E} \\left[\\max\\left(e^{-a\\hat{x}}, Ke^{-b'-ax_{max}}\\right) \\right]e^{b'+ ax_{max}} - K \n",
    "\\end{split}\n",
    "\\end{equation}"
   ]
  },
  {
   "cell_type": "code",
   "id": "514ff089",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-06-18T13:37:06.242002Z",
     "start_time": "2024-06-18T13:37:06.201965Z"
    }
   },
   "source": [
    "import sympy\n",
    "\n",
    "\n",
    "def parse_result_direct(iqae_res):\n",
    "    payoff_expression = f\"sqrt(max({S0[0]} * exp({STEP_X / SCALING_FACTOR * (2 ** (MAX_NUM_QUBITS - MAX_FRAC_PLACES))} * x + ({MU[0]+MIN_X*CHOLESKY[0].sum()})), {K}))\"\n",
    "    payoff_func = sympy.lambdify(sympy.symbols(\"x\"), payoff_expression)\n",
    "    payoff_max = payoff_func(1 - 1 / (2**MAX_NUM_QUBITS))\n",
    "\n",
    "    option_value = iqae_res.estimation * (payoff_max**2) - K\n",
    "    confidence_interval = np.array(iqae_res.confidence_interval) * (payoff_max**2) - K\n",
    "    return (option_value, confidence_interval)"
   ],
   "outputs": [],
   "execution_count": 25
  },
  {
   "cell_type": "markdown",
   "id": "10ab8023",
   "metadata": {},
   "source": [
    "# Run Method"
   ]
  },
  {
   "cell_type": "code",
   "id": "af4639dd",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2024-06-18T13:37:06.278944Z"
    }
   },
   "source": [
    "qmod_direct, qprog_direct, iqae_res_direct, parsed_res_direct = synthesize_and_execute(\n",
    "    parse_result_direct\n",
    ")\n",
    "result, conf_interval = parsed_res_direct\n",
    "print(\n",
    "    f\"raw iqae results: {iqae_res_direct.estimation} with confidence interval {iqae_res_direct.confidence_interval}\"\n",
    ")\n",
    "print(f\"option estimated value: {result} with confidence interval {conf_interval}\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Starting synthesis\n",
      "Opening: http://nightly.platform.classiq.io/circuit/4ebd619d-7799-4255-abe2-9e2d311a8b0d?version=0.43.0.dev22\n",
      "Starting execution\n",
      "raw iqae results: 0.08032238745732972 with confidence interval (0.07819024342750738, 0.08245453148715207)\n",
      "option estimated value: 24.743424470456375 with confidence interval [19.04309702 30.44375192]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Assertions"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "assert -5 <= result <= 45"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## References\n",
    "\n",
    "<a name='QALROP'>[1]</a>: [Francesca Cibrario et al., Quantum Amplitude Loading for Rainbow Options Pricing. Preprint](https://arxiv.org/abs/2402.05574v2)\n"
   ],
   "id": "d0e80f6569d777d2"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
